package com.designoutput.ui
{
	import com.designoutput.manager.ResourceManager;
	import com.designoutput.service.CardService;
	import com.designoutput.service.ServiceError;
	import com.designoutput.ui.controls.Button;
	import com.designoutput.ui.controls.Checkbox;
	import com.designoutput.ui.controls.Input;
	import com.designoutput.ui.controls.Literal;
	import com.designoutput.ui.newControls.DialogBox;
	import com.designoutput.utils.Base64;
	import com.designoutput.utils.Globals;
	import com.designoutput.utils.MessageBoxUtils;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;

	[Event(name="complete", type="flash.events.Event")]
	public class SaveCardDialog extends DialogBox
	{
		private var _cardNameLabel:Literal;
		private var _cardNameInput:Input;
		private var _saveButton:Button;
		private var _overwriteCurrentCard:Checkbox;
				
		override protected function initialize():void {
			super.initialize();
			
			setTitle("Save Card");
			
			_cardNameLabel = new Literal("Card Name:", true);
			addChild(_cardNameLabel);
			
			_cardNameInput = new Input(200, "", 100);
			_cardNameInput.text = Globals.designoutputCard.card.cardName;
			addChild(_cardNameInput);
			
			_overwriteCurrentCard = new Checkbox(ResourceManager.getValue("rm_overrideCurrentCard"));
			_overwriteCurrentCard.check(true);
			addChild(_overwriteCurrentCard);
			
			_saveButton = new Button("Save", 100);
			_saveButton.addEventListener(MouseEvent.CLICK, saveButton_clickHandler, false, 0, true);
			addChild(_saveButton);
			
			width = 300;
			height = 150;
		}
		
		override protected function updateDisplay():void {
			super.updateDisplay();
			
			_cardNameLabel.x = 20;
			_cardNameLabel.y = 30;
			
			_cardNameInput.x = _cardNameLabel.x + _cardNameLabel.width + 2;
			_cardNameInput.y = 32;
			_cardNameInput.width = _width - _cardNameInput.x - 20;
			
			_overwriteCurrentCard.x = _width/2 - _overwriteCurrentCard.width/2;
			_overwriteCurrentCard.y = 62;
			_overwriteCurrentCard.visible = Globals.designoutputCard.card.cardId != 0;
			
			_saveButton.x = _width/2 - _saveButton.width/2;
			_saveButton.y = 92;
		}
		
		protected function saveButton_clickHandler(e:MouseEvent):void {
			if(/^\s*$/.test(_cardNameInput.text)){
				MessageBoxUtils.showMessage(ResourceManager.getValue("rm_informCardName_message"), ResourceManager.getValue("rm_informCardName_title"), 300, 120);
				return;
			}
						
			var cardIdToSave:int = Globals.designoutputCard.card.cardId;
			
			if(_overwriteCurrentCard.visible && !_overwriteCurrentCard.checked)
				cardIdToSave = 0;
			
			var base64PNGThumb:String = Base64.encode(Globals.designoutputCard.canvas.card.getPNGThumb()); 

			var cardXML:XML = Globals.designoutputCard.canvas.card.toXML();
			CardService.instance.saveCard(cardIdToSave, _cardNameInput.text, cardXML.toXMLString(), base64PNGThumb, saveComplete, saveError);
		}
		
		protected function saveComplete(result:Object):void {
			var cardSavedXML:XML = new XML(result);
			
			if(cardSavedXML.localName() == "cardSaved"){
				Globals.designoutputCard.card.setCardData(int(cardSavedXML.@id), String(cardSavedXML.@name));
				
				Globals.designoutputCard.card.resetPendingChanges();
				dispatchEvent( new Event(Event.COMPLETE) );
				close();
			}
		}
		
		protected function saveError(error:ServiceError):void {
			MessageBoxUtils.showMessage(error.message, ResourceManager.getValue("rm_error_message_title"), 500, 140);
		}
		
		override protected function onCloseClick():void {
			close();
		}
		
		override protected function onConfirmKey():void {
			saveButton_clickHandler(null);
		}
		
		override protected function onCancelKey():void {
			onCloseClick();
		}
	}
}